package com.cimp.controller.iqy;

import com.cimp.domain.common.Result;
import com.cimp.domain.iqy.Virtual;
import com.cimp.domain.iqy.call.Call;
import com.cimp.service.iqy.IqyService;
import com.cimp.util.iqy.IqyContants;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

@RestController
@RequestMapping("iqy")
public class IqyController {

    @Resource
    private IqyService iqyService;

    /**
     * 返回token
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "token")
    public String generateToken(){
        String token = iqyService.generateToken();
        return token;
    }


    /**
     * 增加接口模拟
     * @return
     */
    @RequestMapping(value = "virtual/add")
    public Result addVirtualInterface(Virtual virtual, HttpServletRequest request) throws MalformedURLException {
        StringBuffer requestURL = request.getRequestURL();
        URL url = new URL(requestURL.toString());
        String authority = url.getAuthority();
        if (virtual.getId() == null) {
            String iqyApi = IqyContants.interfaceMap.get(virtual.getRegion());
            String path = new URL(iqyApi).getPath();
            virtual.setUrl(url.getProtocol()+"://"+authority +"/iqy/api"+ path);
            iqyService.insertVirtual(virtual);
        }else{
            iqyService.updateVirtual(virtual);
        }
        return Result.success();
    }

    /**
     * 获取虚拟接口列表
     */
    @RequestMapping(value = "virtual")
    public List<Virtual> queryVirtualInterface(){
        List<Virtual> virtuals = iqyService.queryVirtualInterface();
        return virtuals;
    }

    /**
     * 删除虚拟接口
     */
    @RequestMapping(value = "virtual/delete")
    public Result deleteVirtualInterface(Integer id){
        iqyService.deleteVirtualInterface(id);
        return Result.success();
    }

    /**
     * 激活去激活虚拟接口
     */
    @RequestMapping(value = "virtual/active")
    public Result activeVirtualInterface(Virtual virtual){
        iqyService.updateVirtualState(virtual);
        return Result.success();
    }

    @RequestMapping(method = RequestMethod.GET, value = "/api/{xxx}" )
    public String virtualInterface(@PathVariable("xxx") String api){
        String content = iqyService.queryVirtualContent(api);
        if (StringUtils.isBlank(content)) {
            content = "{\"code\":\"C00002\"}";
        }
        return content;
    }

    /**
     * 爱奇艺接口调用
     */
    @RequestMapping(value = "/api/call",produces="text/html;charset=UTF-8")
    public String callInterface(Call call) throws JsonProcessingException {
        String name = call.getName();
        if (StringUtils.isBlank(name)) {
            throw new RuntimeException("interface name is empty");
        }
        String result = this.iqyService.callInterface(call);

        return result;
    }
}
